package com.qf.boot2203.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.qf.boot2203.dao.UserDao;
import com.qf.boot2203.entity.User;
import com.qf.boot2203.ex.UserException;
import com.qf.boot2203.service.UserService;
import com.qf.boot2203.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class UserServiveImpl implements UserService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserDao userDao;

    /**
     * 用户头像上传路径
     */
    @Value("${qfsy.Profilephotopath}")
    private String Profilephotopath;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void registUser(UserRegistVo userRegistVo) throws IOException {

        //1.检查数据库是否存在相同姓名的用户

        User user = userDao.getOneUserByName(userRegistVo.getUsername());

        if(user!=null){
            throw new UserException("-1001","用户名已存在");
        }

        //2.保存文件到指定目录

        final MultipartFile photo = userRegistVo.getPhoto();

        //2.1 生成一个唯一的文件名
        final String prefix = UUID.randomUUID().toString();
        final String fileName = prefix + "_" + photo.getOriginalFilename();
        final String filePath = Profilephotopath + fileName;

        //3.上传头像到指定目录
        photo.transferTo(new File(filePath));

        log.debug("上传头像成功,filePath={}",filePath);

        //4. 把用户的数据写入数据库表
        final User insertUserToDb = new User();

        insertUserToDb.setName(userRegistVo.getUsername());

        //4.1将密码转为hd5加密格式
        String userpass = userRegistVo.getPassword();

        String md5Hexpass = DigestUtil.md5Hex(userpass);

        insertUserToDb.setPassword(md5Hexpass);

        insertUserToDb.setSex(userRegistVo.getSex());

        insertUserToDb.setBirthday(userRegistVo.getDate());

        insertUserToDb.setRegistTime(new Date());

        insertUserToDb.setPhotopath(fileName);

        userDao.insertUser(insertUserToDb);

        log.debug("用户数据落库成功,user={}",user);

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User login(String username, String password) {

        //1. 根据用户名查询用户信息

        User user =  userDao.getOneUserByName(username);

        if (user == null){  // 没用匹配的用户，直接返回null
            return null;
        }else {
            //2. 比对密码是否正确
            final String dbpassword = user.getPassword();

            // 转换 password 为 md5 字符串
            final String md5password = DigestUtil.md5Hex(password);

            if (dbpassword.equals(md5password)){
                return user;
            }

            return null;
        }

    }

    @Override
    public String hello() {
        return "hello";
    }
}
